package com.hblack.manage.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.hblack.common.bean.EasyUIResult;
import com.hblack.manage.pojo.Content;
import com.hblack.manage.service.ContentService;

@Controller
@RequestMapping(value = "content")
public class ContentController {
	
	@Autowired
	private ContentService contentService;

	@RequestMapping(method = RequestMethod.POST)
	public ResponseEntity<Void> addContent(Content content){
		
		try {
			content.setId(null);
			Integer saveCount = this.contentService.save(content);
			if(saveCount == 1){
				return ResponseEntity.status(HttpStatus.CREATED).build();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
	
	@RequestMapping(method = RequestMethod.GET)
	public ResponseEntity<EasyUIResult> queryContentByPage(@RequestParam("categoryId")Long categoryId,@RequestParam(value = "page", defaultValue = "1")Integer pageNum,@RequestParam("rows")Integer pageSize){
		
		try {
			EasyUIResult result = this.contentService.queryContentByCategoryId(categoryId,pageNum,pageSize);
			if(result == null ){
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
			}else {
				return ResponseEntity.ok(result);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}
	
	@RequestMapping(value = "edit", method = RequestMethod.POST)
	public ResponseEntity<Void> updadeContent(Content content){
		
		try {
			Integer count = this.contentService.updateByPrimaryKey(content);
			if(count == 1) {
				return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
}
